package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Cmf_Product;
import cn.wolfcode.trip.base.domain.Cmf_User_Product;
import cn.wolfcode.trip.base.query.ProductObject;
import cn.wolfcode.trip.base.service.ICmf_ProductService;
import cn.wolfcode.trip.base.service.ICmf_User_ProductService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.alibaba.druid.VERSION;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 该类中存放的是所有的旅游商品
 */
@Controller
@RequestMapping("/products")
public class CmfProduct {
    @Autowired
    private ICmf_ProductService productService;
    @Autowired
    private ICmf_User_ProductService user_productService;
    @Autowired
    private ServletContext ctx;
    /**
     * 查询出所有的旅游商品
     * @return
     */
    @GetMapping("/{currentPage}/selectForList")
    @ResponseBody
    public Object selectForList(ProductObject qo){
        PageInfo pageInfo = productService.selectForList(qo);
        return pageInfo;
    }

    /**
     * 根据指定的商品id查询出商品详细信息
     * @param productId
     * @return
     */
    @ResponseBody
    @RequestMapping("/{productId}/detail")
    public Object selectByProductId(@PathVariable Long productId){
        Cmf_Product cmf_product = productService.selectByProductId(productId);
        return cmf_product;
    }

    /**
     * 该方法用来表示用户将商品添加到购物车去
     * @return
     */
    @ResponseBody
    @PutMapping("/{userId}/{productId}/save")
    public Object saveProduce(Cmf_User_Product cmf_user_product){
        JSONResult jsonResult = new JSONResult();
        try{
            user_productService.save(cmf_user_product);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }


    /**
     * 该方法用来处理退款的事情
     * @param cmf_user_product
     * @return
     */
    @ResponseBody
    @PutMapping("/{userId}/{productId}/refund")
    public Object  refund(Cmf_User_Product cmf_user_product){
        JSONResult jsonResult = new JSONResult();
        try{
            user_productService.refund(cmf_user_product);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }


    /**
     * 该方法根据用户的id查询该用户购物车的所有商品（包括未支付跟已支付）
     * @return
     */
    @ResponseBody
    @GetMapping(value="/{userId}/product",produces = "application/json;charset=utf-8")
    public Object getUserProduct(ProductObject qo){
        PageInfo pageInfo = user_productService.selectByUserId(qo);
        return pageInfo;
    }

    /**
     * 使用freemarker来拼接表格,进行遍历
     * @param qo
     * @param resp
     */
    @GetMapping(value="/{userId}/{currentPage}/product",produces = "text/html;charset=utf-8")
    public void  getUserProduct(ProductObject qo, HttpServletResponse resp){
        resp.setCharacterEncoding("UTF-8");
        //创建一个模板配置对象
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        //设置模板路径
        try {
            configuration.setDirectoryForTemplateLoading(new File(ctx.getRealPath("templates")));
            //设置模板的编码格式
            configuration.setDefaultEncoding("utf-8");
            //获取到模板
            Template template = configuration.getTemplate("shopping_cart.ftl");
            //查询出所有数据
            PageInfo pageInfo = user_productService.selectByUserId(qo);
            //将数据输出打印到页面上去
            Map<String,Object> map=new HashMap<>();
            map.put("pageInfo",pageInfo);
            template.process(map,resp.getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
